package soulkun.library.component

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory
import androidx.lifecycle.ViewModelProvider.NewInstanceFactory
import soulkun.library.utils.log.LogUtils

abstract class BaseFragment(private val isNeedSavedView: Boolean, private val isNeedLogLifecycle: Boolean = false): Fragment() {

    companion object{
        var TAG = "BaseFragment"
    }

    protected var rootView: View? = null
    protected lateinit var mContext: Context
    protected lateinit var mActivity: AppCompatActivity
    protected lateinit var mFragment: Fragment
    protected lateinit var activityFragmentManager: FragmentManager

    override fun onAttach(context: Context) {
        super.onAttach(context)
        TAG = "TAG:" + javaClass.simpleName
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onAttach")
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onCreate")
        mFragment = this
        doOnCreate()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onCreateView")
        return if (isNeedSavedView) {
            if (rootView == null) {
                rootView = inflater.inflate(setLayout(), null)
            }
            rootView
        }else{
            rootView = inflater.inflate(setLayout(), null)
            rootView
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onViewCreated")
        mActivity = activity as AppCompatActivity
        activityFragmentManager = mActivity.supportFragmentManager
        initView()
        initData()
        initListener()
    }

    override fun onStart() {
        super.onStart()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onStart")
    }

    override fun onResume() {
        super.onResume()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onPause")
    }

    override fun onStop() {
        super.onStop()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onStop")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onDestroy")
    }

    override fun onDetach() {
        super.onDetach()
        if (isNeedLogLifecycle) LogUtils.logE(TAG, "onDetach")
    }

    protected fun <T : View?> findViewById(@IdRes id: Int): T {
        return rootView!!.findViewById(id)
    }

    @LayoutRes
    protected abstract fun setLayout(): Int

    protected abstract fun initView()

    protected abstract fun initData()

    protected abstract fun initListener()

    protected abstract fun doOnCreate()

}